local ma__mozhi = fk.CreateSkill {
  name = "ma__mozhi",
}

Fk:loadTranslationTable {
  ["ma__mozhi"] = "默识",
  [":ma__mozhi"] = "出牌阶段结束时，你可以将一张牌当本轮被使用过的基本牌或普通锦囊牌使用，若你此阶段使用过此牌名的牌，你可以重复此流程(不能为此阶段已选择过的牌名)。",

  ["#ma__mozhi-use"] = "默识：你可以将一张牌当其中一张基本牌或普通锦囊牌使用",

  ["$ma__mozhi1"] = "你我姐妹，不妨今日一比。",
  ["$ma__mozhi2"] = "儿时所学，至今犹可诵忆。",
}

local round_names = {}
local my_names = {}

local myTurnFuc = function(room, player, skill_names)
  room.logic:getEventsOfScope(GameEvent.UseCard, math.maxinteger, function(e)
    local use = e.data
    if not table.contains(round_names, use.card.name) and (use.card.type == Card.TypeBasic or use.card:isCommonTrick()) then
      table.insert(round_names, use.card.name)
      if skill_names then
        table.insert(skill_names, use.card.name)
      end
    end
  end, Player.HistoryRound)

  --放入my_names，仅出牌阶段，且必须是本人使用
  room.logic:getEventsOfScope(GameEvent.UseCard, math.maxinteger, function(e)
    local use = e.data
    if use.from == player and (use.card.type == Card.TypeBasic or use.card:isCommonTrick()) then
      table.insertIfNeed(my_names, use.card.name)
    end
  end, Player.HistoryPhase)
end

ma__mozhi:addEffect(fk.RoundStart, {
  can_refresh = function()
    return #round_names > 0
  end,
  on_refresh = function()
    round_names = {}
  end
})

ma__mozhi:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(ma__mozhi.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if target == player and player:hasSkill(ma__mozhi.name) then
      room.logic:getEventsOfScope(GameEvent.UseCard, math.maxinteger, function(e)
        local use = e.data
        if use.card.type == Card.TypeBasic or use.card:isCommonTrick() then
          table.insertIfNeed(round_names, use.card.name)
        end
      end, Player.HistoryTurn)
    end
  end
})

ma__mozhi:addEffect(fk.EventPhaseEnd, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(ma__mozhi.name) and player.phase == Player.Play and not player:isNude() then
      my_names = {}
      local room = player.room
      myTurnFuc(room, player)
      return #round_names > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use
    local skill_names = table.clone(round_names)
    while true do
      use = room:askToUseVirtualCard(player, {
        name = skill_names,
        skill_name = ma__mozhi.name,
        prompt = "#ma__mozhi-use",
        cancelable = true,
        extra_data = {
          extraUse = true,
        },
        card_filter = {
          n = 1,
          cards = player:getCardIds("he"),
        },
      })

      if use then
        local temp_names = table.clone(skill_names)
        for index, name in ipairs(temp_names) do
          local name_splited = name:split("__")
          name = name_splited[#name_splited]

          if use.card.trueName == name then
            temp_names[index] = "-1"
            --table.remove(skill_names, index)
          end
        end

        skill_names = {}
        --[[
        local str1 = table.concat(temp_names, ", ")
        print("temp_names:" + str1)]]
        
        for index, name in ipairs(temp_names) do
          if temp_names[index] ~= "-1" then
            table.insert(skill_names, name)
          end
        end

        --再一次检索，封装函数来优化代码
        --myTurnFuc(room, player, skill_names)
      end

      if not use or not table.contains(my_names, use.card.name) or player:isNude() then
        break
      end
    end
  end,
})



return ma__mozhi
